package com.wangdong.mijiqiqi.threetype;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.wangdong.mijiqiqi.R;


/**
 * 作者：wangdong on 2018/10/19 18:10
 * 邮箱：2100520956@qq.com
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
    protected Context mContext = BaseActivity.this;
    private BaseParams getParams;
    private BaseParams setParams;

    public BaseParams getParams() {
        return getParams;
    }

    public BaseParams setParams() {
        return setParams;
    }

    public Context getmContext() {
        return mContext;
    }

    public void setmContext(Context mContext) {
        this.mContext = mContext;
    }

    private ActionBarFragment actionBarFragment;

    @Override
    protected void onStart() {
        super.onStart();
        try {
            setParams = new BaseParams();
            if (getIntent().getSerializableExtra(Constancts.parames) != null) {
                getParams = (BaseParams) getIntent().getSerializableExtra(Constancts.parames);
            }
            if (getParams == null) {
                getParams = new BaseParams();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {

            setContentView(getContentResId());
            initHeadView();
            initView();
            mContext = this;
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    protected abstract int getContentResId();

    protected abstract void initView();

    public void startActivityNoFinish(Class<?> cls) {
        startActivity(new Intent(BaseActivity.this, cls).putExtra(Constancts.parames, setParams));
    }

    public void startActivity(Class<?> cls) {
        startActivity(new Intent(BaseActivity.this, cls).putExtra(Constancts.parames, setParams));
        finish();
    }

    /* <include layout="@layout/fragment_title"/>*/
    public void initHeadView() {
        actionBarFragment = (ActionBarFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_title);
        //左边点击
        actionBarFragment.setLeftButton(R.mipmap.icon_back, new ActionBarFragment.IActionBarLeftOnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        if (getRightName() != -1) {
            //右边点击
            actionBarFragment.setRightBtnName(getRightName(), new ActionBarFragment.IActionBarRightBtnOnClickListener() {

                @Override
                public void onClick(View view) {
                    submitClick();
                }
            });
        }
        if (getRightImg() != -1) {
            actionBarFragment.setRightBtnImage(getRightImg(), new ActionBarFragment.IActionBarRightBtnOnClickListener() {

                @Override
                public void onClick(View view) {
                    submitClick();
                }
            });
        }
        if (getBackgroundColor() != -1) {
            actionBarFragment.setBackgroundColor(getBackgroundColor());
        }
        actionBarFragment.setTitleName(getSubTitle());
    }

    public int getBackgroundColor() {
        return -1;
    }

    public int getRightName() {
        return -1;
    }

    public int getRightImg() {
        return -1;
    }

    public void submitClick() {

    }

    protected abstract String getSubTitle();

    @Override
    public void onClick(View v) {

    }
}
